<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_COMPONENTS_PATH."base_display_component_class.php");
	require_once(CUTEENGINE_PARTS_PATH."table_part/table_part.php");
    //
    // t_component_table

    //
	class t_component_table extends t_base_display_component
	{	
		// property
		// public		
		// C
		public $content_loader = null;
		// G
		const language_position_last = 1;
		
        //
        // M
       /* const main_query_component_type = 'type';
		const main_query_component_selection_query = 'selection_query';
        const main_query_component_selection_query_condition = 'selection_query_condition';
		const main_query_component_selection_query_fiels_to_order = 'fiels_to_order';
        const main_query_component_update_query = 'update_query';
        const main_query_component_insert_query = 'insert_query';
        const main_query_component_delete_query = 'delete_query';
        const main_query_component_events = 'events';
        //const main_query_component_id = 'component_id';
		const main_query_component_special_columns = 'special_columns';
		const main_query_component_multi_action_form = 'multi_action_form';
		const main_query_component_results_count = 'results_count';*/
        
		// S
        const specific_parameters_position_type = 1;
		const specific_parameters_position_selection_query = 2;
        const specific_parameters_position_selection_query_conditions = 3;
		const specific_parameters_position_selection_query_fields_to_order = 4;
        const specific_parameters_position_update_query = 5;
        const specific_parameters_position_insert_query = 6;
        const specific_parameters_position_delete_query = 7;
        const specific_parameters_position_events = 8;
		const specific_parameters_position_special_columns = 9;
		const specific_parameters_position_multi_action_form = 10;
		const specific_parameters_position_results_count = 11;		
        const specific_parameters_position_last = 12;
		
		// method
		// protected
		//C
		protected function create_tags_structure()
		{
			global $g_javascript_namespace;            
            
            // javascript
			// C
            //
            if (! $this->main_tag)
			{
                
				$this->main_tag = new t_base_tag(t_base_tag_type::btt_div,$this->name);

				$local_header_script_generator = $this->main_tag->header_script_generator;
				// STYLE
                $this->main_tag->include_child_str = true;
				//$local_header_script_generator->add_style_sheet_property("#$this->name","left","5px");
				//$local_header_script_generator->add_style_sheet_property("#$this->name","top","5px");
                $local_header_script_generator->add_style_sheet_property("#$this->name","display","inline-block");
				$local_header_script_generator->add_style_sheet_property("#$this->name",'position','relative');
				$local_header_script_generator->add_style_sheet_property("#$this->name",'background-color','lightblue');
				$local_header_script_generator->add_style_sheet_property("#$this->name",'border','1px solid blue');
					
				$this->main_tag->set_editing_tags(true);
                //
				$this->compute_tags_structure();
                if ($this->use_log)
                {
                    $this->main_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_div,parent::get_log_tag_name());
                }    
                /* */
                
			}
		}
        // G
		public function get_language_position_last()
        {
            return self::language_position_last;
        }
        static function get_help_specific_parameter($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::specific_parameters_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_specific_parameter($a_index)
		{
			$local_return = '';
			//echo "a_index: $a_index<br>";
			switch ($a_index) 
            {
                case self::specific_parameters_position_multi_action_form:
					$local_return = 'FALSE';
				break;  
				case self::specific_parameters_position_results_count:
					$local_return = '10';
				break;              
			}
			return $local_return;
		}
        protected function get_specific_parameters_position_last()
        {
            return self::specific_parameters_position_last;
        }
		// I
		protected function initialize_query_parameters()
		{
            /*$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_type,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				self::specific_parameters_position_type);	
            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_selection_query,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				self::specific_parameters_position_selection_query);	
            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_selection_query_condition,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				self::specific_parameters_position_selection_query_conditions);	    
            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_selection_query_fiels_to_order,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				self::specific_parameters_position_selection_query_fields_to_order);
			$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_update_query,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				self::specific_parameters_position_update_query);	
            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_insert_query,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				self::specific_parameters_position_insert_query);	
            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_delete_query,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				self::specific_parameters_position_delete_query);					
            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_events,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				self::specific_parameters_position_events);	
            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_special_columns,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				self::specific_parameters_position_special_columns);	
			$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_multi_action_form,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				self::specific_parameters_position_multi_action_form);	
			$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_results_count,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				self::specific_parameters_position_results_count);		*/		
		}
		// public
		public function __construct($a_cuteengine_component,$a_query_parameter) 
		{
			global $g_javascript_namespace;
			$this->use_log = false;
            

			$this->content_loader = new t_content_loader($g_javascript_namespace,$this);
			$this->content_loader->initialize_parameter('undefined',
				'undefined','undefined','undefined',
				'undefined','undefined',false);
			
            parent::__construct($a_cuteengine_component,$a_query_parameter);
			$this->content_loader->component_to_log = $this->get_log_tag_name();

		}	
		// C
		public function create_internal_parts()
        {
			global $g_database_namespace;
            parent::create_internal_parts();
			//echo __METHOD__  . "<br>";
            $local_sql = $this->get_specific_parameters_position_selection_query();
			$local_type = $this->get_specific_parameters_position_type();
			
            $local_condition = $this->get_specific_parameters_position_selection_query_conditions();
            //echo $this->get_specific_parameters_position_insert_query()."<br>";
			if ($local_condition != '')
            {
                if (strpos(strtoupper($local_sql),' WHERE '))
                    $local_sql = $local_sql . " and ($local_condition)";
                else
                    $local_sql = $local_sql . " WHERE ($local_condition)"; 
            }
			$local_fiels_to_order = $this->get_specific_parameters_position_selection_query_fields_to_order();
			if ($local_fiels_to_order != '')
            {
				$local_sql = $local_sql . " ORDER BY $local_fiels_to_order"; 
            }
            //echo "local_sql: $local_sql<br>";
            //echo "this->get_specific_parameters_position_special_columns():".$this->get_specific_parameters_position_special_columns()."<br>";
            // PARENT PART
            $local_part_name = '';
            /*$local_component = $g_cuteengine->components[$this->get_specific_parameters_position_parent_component_id()]
					[t_cuteengine::components_cuteengine_component];
            if (isset($local_component))
            {
                $local_component = $local_component->page_component;
                $local_part = $local_component->get_main_part();
                if (isset($local_part))
                {
                    $local_part_name = $local_part->part_name;
                }
            }  */  
			//echo "this->get_condition_values(): ".$this->get_condition_values()."<br>";   //|true|true|true|true|true|true|true|true|
			// TABLE PARTS
			//echo "this->get_specific_parameters_position_results_count(): ".$this->get_specific_parameters_position_results_count()."<br>";
            $local_part = $this->add_new_part(-1,1,t_table_part,'@'.$this->name.'_table@'.$this->get_pspecific_parameters_position_events().'@'.$local_part_name.'@',
                "@".$local_sql."@".$this->get_condition_values()."@",
				//field 1
				"@|t_field_grid|".$this->name."_table_tag|".t_table_part::fields_position_table."|$local_type|".$this->get_specific_parameters_position_special_columns()."|".$this->get_specific_parameters_position_multi_action_form()."|@",
				// action 1
				'@["none","t_action_set_values_from_db","'.$this->name.'_table_get_data",['.
                '"","","'.$this->name.'_table_tag",""],"'.$local_sql.'",'.$this->get_json_condition_values().',['.
				'""],[""],"'.$this->get_specific_parameters_position_results_count().'"]@'
                ,1);  
            
           
			//
        
   
        }
		
		//D
	/*	public function display_component()
		{
			parent::display_component();
		}*/
		// E
		public function execute_component_action()
		{
			$local_function = parent::execute_component_action();
            if ($local_function)
			{
				$this->$local_function();
            }
			else
			{
				 
			}
			//echo "argomento: ".$this->url_argument(self::main_url_argument_import_file_name);
			//$this->parse_source();
		}       
        // G
        public function get_condition_values()
        {
            global $g_url_parameters_namespace;
            //echo "this->id: $this->id<br><br>";
			$local_parameter = $g_url_parameters_namespace->get_parameter_by_component_id($this->id);
			//echo "local_parameter->argument: " . $local_parameter->argument ."<br>";
			
            //print_r($local_parameter);echo "<br>";
			if ($local_parameter)
			    return t_strings_utility_namespace::separator_level_2.
					str_replace(t_url_parameter::separator_level_0, t_strings_utility_namespace::separator_level_2, $local_parameter->argument).
					t_strings_utility_namespace::separator_level_2;
			else
				return '';
             
			//return self::main_query_component_button_label;
        }   
        public function get_json_condition_fileds()
        {
            $local_conditions_array = explode(':',
				$this->get_specific_parameters_position_selection_query_conditions());   
			$local_result = "[";
			foreach($local_conditions_array as $local_element_index => $local_element)
			{
				if (($local_element_index % 2) == 1)
					if ($local_element_index == 1) 
						$local_result .= '"'.$local_element.'"';
					else
						$local_result .= ',"'.$local_element.'"';		
			}
			$local_result .= "]";
            return $local_result;
		
        }
        public function get_json_condition_values()
        {
            global $g_url_parameters_namespace;
			$local_parameter = $g_url_parameters_namespace->get_parameter_by_component_id($this->id);
			if (! isset($local_parameter) || (trim($local_parameter->argument) == ''))
				$local_parameter = $g_url_parameters_namespace->get_parameter_by_position(0); 
				
			//echo "local_parameter->argument:$local_parameter->argument<br>";
			
			if ($local_parameter)
			{
			    $local_array = explode(t_url_parameter::separator_level_0,$local_parameter->argument);
			}
			else
            {
				$local_array = array();
  //              
            }    
            return str_replace('\/','/',json_encode($local_array));
        }        
        public function get_specific_parameters_position_type()
        {
            return $this->specific_parameters[self::specific_parameters_position_type];
        }        
        public function get_specific_parameters_position_selection_query()
        {
            return $this->specific_parameters[self::specific_parameters_position_selection_query];
        }        
	    public function get_specific_parameters_position_selection_query_conditions()
        {
            return $this->specific_parameters[self::specific_parameters_position_selection_query_conditions];
        } 	    
		public function get_specific_parameters_position_selection_query_fields_to_order()
        {
            return $this->specific_parameters[self::specific_parameters_position_selection_query_fields_to_order];
        } 
        public function get_specific_parameters_position_update_query()
        {
            return $this->specific_parameters[self::specific_parameters_position_update_query];
        } 
        public function get_specific_parameters_position_insert_query()
        {
            return $this->specific_parameters[self::specific_parameters_position_insert_query];
        } 
        public function get_specific_parameters_position_delete_query()
        {
            return $this->specific_parameters[self::specific_parameters_position_delete_query];
        }
		public function get_pspecific_parameters_position_events()
        {
            return $this->specific_parameters[self::specific_parameters_position_events];
        }
		public function get_specific_parameters_position_special_columns()
        {
            return $this->specific_parameters[self::specific_parameters_position_special_columns]; 
        }
		public function get_specific_parameters_position_multi_action_form()
        {
            return $this->specific_parameters[self::specific_parameters_position_multi_action_form]; 
        }
		public function get_specific_parameters_position_results_count()
        {
            return $this->specific_parameters[self::specific_parameters_position_results_count]; 
        }
	   /* public function get_specific_parameters_position_parent_component_id()
        {
            return trim($this->get_main_query_parameter_value(self::main_query_parent_component_id)); 
        }*/
    }
?>